package com.lifeng.job;

import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.IJobHandler;
import com.xxl.job.core.handler.annotation.JobHandler;
import com.xxl.job.core.log.XxlJobLogger;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * Created by lifeng on 2019/9/12.
 * - 1、继承"IJobHandler"：“com.xxl.job.core.handler.IJobHandler”；
 - 2、注册到Spring容器：添加“@Component”注解，被Spring容器扫描为Bean实例；
 - 3、注册到执行器工厂：添加“@JobHandler(value="自定义jobhandler名称")”注解，注解value值对应的是调度中心新建任务的JobHandler属性的值。
 - 4、执行日志：需要通过 "XxlJobLogger.log" 打印执行日志；
 */
@Component
@JobHandler(value="lifengJobHandler")
public class LifengJobHandler extends IJobHandler {
    private static final Logger log = LoggerFactory.getLogger(LifengJobHandler.class);
    @Override
    public ReturnT<String> execute(String s) throws Exception {
        log.debug("lifengJob定时任务开始执行,参数:"+s);
        log.debug("我开始执行了"+new Date().toLocaleString());
        XxlJobLogger.log("lifengJob定时任务开始执行,参数:"+s);
        Integer num = Integer.parseInt(s);
        try{
            int add = 2/num;
        }catch (Exception e){
            XxlJobLogger.log("执行异常"+e.getMessage());
            throw e;
        }
        return new ReturnT<String>(200, "执行成功");
    }
}
